using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Supply
{
	public bool show = false;
	public GameObject prefab;
	public int count = 0;
	public bool isChildren = true;
}

public class DOM : MonoBehaviour 
{
	private static bool hasSearched = false;
	
	#region SINGLETON
	private static DOM instance;
	public static DOM Instance
	{
		get
		{ 
			if(null == instance)
			{
				Debug.Log("DOM created");
				instance = new GameObject("DOM").AddComponent<DOM>();
			}
			return instance;
		}
	}
	public static void Kill(){
		Debug.Log("DOM Killed");
		instance = null;
	}
	public bool dontDestroyOnLoad = false;
	#endregion
	
	public List<Supply> allSupplies = new List<Supply>();
	public List<KeyValuePair<string, GameObject>> stocks = new List<KeyValuePair<string, GameObject>>();
	//public Dictionary<string, GameObject> stocks;
	
	private int supplyId = 0;
	private int totalObject = 0;
	
	#region Private
	private void Awake()
	{
		if(instance == null)
		{
			GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
			bool foundOne = false;
			foreach(GameObject go in gos)
			{
				DOM sm = go.GetComponent<DOM>();
				if(sm)
				{
					if(go == this.gameObject)
					{
						instance = sm;
						foundOne = true;
					}
					else
					{
						Debug.LogError("There is more than one Singleton " + go.name + "Do not use DOM.Instance in Awake functions.");
						DestroyImmediate(go);
					}
				}
			}
		}
		
		transform.position = Vector3.zero;
		stocks = new List<KeyValuePair<string, GameObject>>();
		
		foreach(Supply supply in allSupplies)
		{
			for(int i = 0; i < supply.count; i++)
			{
				if(supply.isChildren)
					CreateObject(supply.prefab, transform);
				else
					CreateObject(supply.prefab, null);
			}
			
			totalObject += supplyId;
			supplyId = 0;
		}
	}
	
	private GameObject CreateObject(string prefabName)
	{
		foreach(Supply supply in allSupplies)
		{
			if(supply.prefab.name == prefabName)
			{
				if(supply.isChildren)
					return CreateObject(supply.prefab, transform);
				else
					return CreateObject(supply.prefab, null);
			}
		}
		Debug.LogError(prefabName + " is not in the pool");
		return null;
	}
	
	private GameObject CreateObject(GameObject prefab, Transform parent)
	{
		if(prefab == null)
		{
			
		}
		
		// Create the object
		GameObject temp = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
		temp.name = prefab.name + totalObject;
		temp.SetActiveRecursively(false);
		
		// Set the new object child of the DOM
		temp.transform.parent = parent;
		
		
		// Stock for future reference
		stocks.Add( new KeyValuePair<string, GameObject>(prefab.name, temp) );
		
		// Increment object unique ID
		supplyId++;
		
		return temp;
	}
	
	private void Start(){
		if(dontDestroyOnLoad){
			DontDestroyOnLoad(gameObject);
		}
	}
	#endregion
	
	#region Public
	public GameObject GetObject(string objectName, Vector3 position, Quaternion rotation)
	{
		// Pour chaque stock
		foreach(KeyValuePair<string, GameObject> kvp in stocks)
		{
			// Si la categorie est celle désiré et que l'object est libre
			if(kvp.Key == objectName && !kvp.Value.active)
			{
				kvp.Value.transform.position = position;
				kvp.Value.transform.rotation = rotation;
				kvp.Value.SetActiveRecursively(true);				
				return kvp.Value;
			}
		}
		
		// Aucun object trouvé créer un nouveau
		GameObject temp = CreateObject(objectName);
		temp.SetActiveRecursively(true);
		
		return temp;
	}
	public void FreeObject(GameObject obj)
	{
		obj.transform.parent = transform;
		obj.transform.position = Vector3.zero;
		obj.SetActiveRecursively(false);
	}
	
	private void OnApplicationQuit()
	{
		instance = null;
	}
	#endregion
}
